package com.binguo.thirdPay.jinQiao;

import com.binguo.common.util.JsonUtil;
import com.binguo.thirdPay.MD5Util;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

public class JinQiaoPayController {

    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    public static void main(String[] args) throws Exception{

        BigDecimal a1 = new BigDecimal(Double.toString(66.85));
        BigDecimal b1 = new BigDecimal(Integer.toString(100));
        BigDecimal result = a1.multiply(b1);
        System.out.println(result.longValueExact());

//        PriceUtil.changeF2Y(1l);

        System.out.println(  Math.round(new Double("66.85")*100));

//        GetCardInfoByMemInterface("03776");
        GetCardInfoByMemInterface("02917");
//        CRMCardPayInterface("60831900154", "1", "asafa");
//        CRMCardPayInterface("60831900154", "1", "afa");
//        System.out.println(new BigDecimal(66.85).multiply(new BigDecimal(100)));
//        GetCardInfoByMemInterface("02001");

//        GetCardInfoByMemInterface("02001");
//        System.out.println(CRMCardPayInterface("02001","1","aa123"));
//        for (int i=0; i< 10; i++) {
//            GetCardInfoByMemInterface("02001");
//        }
    }


    public static JinQiaoPayCardItem GetCardInfoByMemInterface(String cardNo){

        JinQiaoPayCardItem result = new JinQiaoPayCardItem();
        String channelId = "YiYiMeiShiHui";
        String appSecret = "D0EFC17837F4482AB00566F3CF2CC017";
        String groupId = "28131";
        String groupSecret = "8C1C57C4EA02420695C5E0083C9B24BD";


        /**
         * 请求地址：
         * methodName=GetCardInfoByMemInterface&channel=YiYiMeiShiHui&groupID=28131&groupSecret=8C1C57C4EA02420695C5E0083C9B24BD&QueryString=dadaf2&appSecret=D0EFC17837F4482AB00566F3CF2CC017 进行md5加密作为sign的值
         *
         * https://api.caimomo.com/APIHandler.ashx?methodName=GetCardInfoByMemInterface&channel=YiYiMeiShiHui&groupID=28131&groupSecret=8C1C57C4EA02420695C5E0083C9B24BD&QueryString=dadaf2&sign=0a8e2b6860222c6a83f27108cd965fee
         * https://api.caimomo.com/APIHandler.ashx?methodName=GetCardInfoByMemInterfacechannel%3DYiYiMeiShiHui&groupID=28131&groupSecret=8C1C57C4EA02420695C5E0083C9B24BD&queryString=13817246599&sign=cb39b39873981933a4541dc01187cbab
         *
         */

        String url = "https://api.caimomo.com/APIHandler.ashx?";
        String methodName = "GetCardInfoByMemInterface";

        /**
         * methodName=方法名channel=渠道名&groupID=集团号&groupSecret=集团密钥【&其他接口参数(接口参数里面已经在前面有了该参数的，不要重复传参数)】&appSecret=商户密钥
         */

        String param = "methodName=" + methodName +
                "&channel=" + channelId +
                "&groupID=" + groupId +
                "&groupSecret=" + groupSecret +
                "&queryString=" + cardNo;

        String paramWithAppSecret = param +
                "&appSecret=" + appSecret ;


        String sign = MD5Util.MD5Lower32(paramWithAppSecret);


        param += "&sign=" + sign;



        try {
            ResponseEntity<String> responseEntity = new RestTemplate().getForEntity(url + param, String.class);
            System.out.println(responseEntity.getBody());
            Map<String, Object> respBody = JsonUtil.parse(responseEntity.getBody());

            List<Map<String, Object>> data = (List<Map<String, Object>>)respBody.get("Data");

            Double balance = (Double)data.get(0).get("CurrentMoney");


            result.setCurrentMoney(Math.round(balance*100));
            result.setShowName(data.get(0).get("ShowName").toString());
            result.setSex(Integer.parseInt(data.get(0).get("Sex").toString()));
            result.setMobilePhone(data.get(0).get("MobilePhone").toString());
            result.setTrueName(data.get(0).get("TrueName").toString());
            System.out.println(balance+"     " +result.getTrueName()+ "    "+result.getBirthDay());


            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String CRMCardPayInterface(String cardNO,String payMoney,String orderCode){
        String channelId = "YiYiMeiShiHui";
        String appSecret = "D0EFC17837F4482AB00566F3CF2CC017";
        String groupId = "28131";
        String groupSecret = "8C1C57C4EA02420695C5E0083C9B24BD";


        /**
         * 请求地址：
         * methodName=GetCardInfoByMemInterface&channel=YiYiMeiShiHui&groupID=28131&groupSecret=8C1C57C4EA02420695C5E0083C9B24BD&QueryString=dadaf2&appSecret=D0EFC17837F4482AB00566F3CF2CC017 进行md5加密作为sign的值
         *
         * https://api.caimomo.com/APIHandler.ashx?methodName=GetCardInfoByMemInterface&channel=YiYiMeiShiHui&groupID=28131&groupSecret=8C1C57C4EA02420695C5E0083C9B24BD&QueryString=dadaf2&sign=0a8e2b6860222c6a83f27108cd965fee
         * https://api.caimomo.com/APIHandler.ashx?methodName=GetCardInfoByMemInterfacechannel%3DYiYiMeiShiHui&groupID=28131&groupSecret=8C1C57C4EA02420695C5E0083C9B24BD&queryString=13817246599&sign=cb39b39873981933a4541dc01187cbab
         *
         */

        String url = "https://api.caimomo.com/APIHandler.ashx?";
        String methodName = "CRMCardPayInterface";

        /**
         * methodName=方法名channel=渠道名&groupID=集团号&groupSecret=集团密钥【&其他接口参数(接口参数里面已经在前面有了该参数的，不要重复传参数)】&appSecret=商户密钥
         */

        String param = "methodName=" + methodName +
                "&channel=" + channelId +
                "&groupID=" + groupId +
                "&groupSecret=" + groupSecret +
                "&storeID=" + "2813111" +
                "&cardNo=" + cardNO +
//                "&cardNo=" + "02002" +
                "&payMoney=" + payMoney +
                "&orderCode=" + orderCode

                ;

        String paramWithAppSecret = param +
                "&appSecret=" + appSecret ;


        String sign = MD5Util.MD5Lower32(paramWithAppSecret);


        param += "&sign=" + sign;

        System.out.println(url + param);

        ResponseEntity<String> responseEntity = new RestTemplate().getForEntity(url + param, String.class);

        System.out.println(responseEntity.getBody());

        JinQiaoPayResultModel jinQiaoPayResultMode = null;
        try {
            jinQiaoPayResultMode = JsonUtil.parse(responseEntity.getBody(), JinQiaoPayResultModel.class);
        } catch (IOException e) {
            return "failed";
        }

        if (jinQiaoPayResultMode.getResultcode() == 0) {
            return "success";
        }else {
            return "failed";
        }

    }
}
